package com.evan.review.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.evan.review.R;

/**
 * Fragment基本生命周期演示Activity
 * 用于展示Fragment的基本生命周期方法
 */
public class BasicLifecycleActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "BasicLifecycleActivity";
    private TextView tvLifecycleLog;
    private LifecycleFragment lifecycleFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_lifecycle);

        initViews();

        // 避免旋转屏幕等重建时重复添加Fragment
        if (savedInstanceState == null) {
            // 创建并添加Fragment
            lifecycleFragment = new LifecycleFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, lifecycleFragment, "lifecycle_fragment")
                    .commit();
        } else {
            // 恢复已有Fragment
            lifecycleFragment = (LifecycleFragment) getSupportFragmentManager()
                    .findFragmentByTag("lifecycle_fragment");
        }
    }

    private void initViews() {
        tvLifecycleLog = findViewById(R.id.tv_lifecycle_log);

        Button btnAdd = findViewById(R.id.btn_add);
        Button btnRemove = findViewById(R.id.btn_remove);
        Button btnReplace = findViewById(R.id.btn_replace);
        Button btnHideShow = findViewById(R.id.btn_hide_show);
        Button btnClearLog = findViewById(R.id.btn_clear_log);

        btnAdd.setOnClickListener(this);
        btnRemove.setOnClickListener(this);
        btnReplace.setOnClickListener(this);
        btnHideShow.setOnClickListener(this);
        btnClearLog.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (id == R.id.btn_add) {
            // 添加Fragment
            if (lifecycleFragment == null) {
                lifecycleFragment = new LifecycleFragment();
                transaction.add(R.id.fragment_container, lifecycleFragment, "lifecycle_fragment");
                transaction.commit();
                appendLog("添加Fragment");
            } else if (fragmentManager.findFragmentByTag("lifecycle_fragment") == null) {
                transaction.add(R.id.fragment_container, lifecycleFragment, "lifecycle_fragment");
                transaction.commit();
                appendLog("重新添加Fragment");
            } else {
                appendLog("Fragment已存在，无需添加");
            }
        } else if (id == R.id.btn_remove) {
            // 移除Fragment
            Fragment fragment = fragmentManager.findFragmentByTag("lifecycle_fragment");
            if (fragment != null) {
                transaction.remove(fragment);
                transaction.commit();
                appendLog("移除Fragment");
            } else {
                appendLog("Fragment不存在，无法移除");
            }
        } else if (id == R.id.btn_replace) {
            // 替换Fragment
            ReplacementFragment replacementFragment = new ReplacementFragment();
            transaction.replace(R.id.fragment_container, replacementFragment, "replacement_fragment");
            transaction.commit();
            appendLog("替换为新的Fragment");

            // 更新引用
            lifecycleFragment = null;
        } else if (id == R.id.btn_hide_show) {
            // 隐藏/显示Fragment
            Fragment fragment = fragmentManager.findFragmentByTag("lifecycle_fragment");
            if (fragment != null) {
                if (fragment.isHidden()) {
                    transaction.show(fragment);
                    appendLog("显示Fragment");
                } else {
                    transaction.hide(fragment);
                    appendLog("隐藏Fragment");
                }
                transaction.commit();
            } else {
                Fragment replacementFragment = fragmentManager.findFragmentByTag("replacement_fragment");
                if (replacementFragment != null) {
                    if (replacementFragment.isHidden()) {
                        transaction.show(replacementFragment);
                        appendLog("显示替换的Fragment");
                    } else {
                        transaction.hide(replacementFragment);
                        appendLog("隐藏替换的Fragment");
                    }
                    transaction.commit();
                } else {
                    appendLog("没有Fragment可以隐藏/显示");
                }
            }
        } else if (id == R.id.btn_clear_log) {
            // 清空日志
            tvLifecycleLog.setText("");
            if (lifecycleFragment != null) {
                lifecycleFragment.clearLog();
            }
            appendLog("日志已清空");
        }
    }

    /**
     * 添加日志
     */
    public void appendLog(String log) {
        if (tvLifecycleLog == null) {
            return;
        }
        if (lifecycleFragment == null) {
            return;
        }
        String currentLog = tvLifecycleLog.getText().toString();
        tvLifecycleLog.setText(currentLog + "\n[Activity] " + log);
    }

    /**
     * Fragment调用此方法添加日志
     */
    public void appendFragmentLog(String log) {
        if (tvLifecycleLog == null) {
            return;
        }
        if (lifecycleFragment == null) {
            return;
        }
        String currentLog = tvLifecycleLog.getText().toString();
        tvLifecycleLog.setText(currentLog + "\n" + log);
    }
} 